﻿using System;
using System.Data;
using System.Diagnostics;
using Habanero.BO;
using Habanero.BO.ClassDefinition;
using Habanero.Util;
using UseCaseManager.BO;
namespace UseCaseManager.Logic
{
    public class CreateAndExportTable
    {
        public static void ExportToCSV(Release release)
        {
            if (release == null) throw new ArgumentNullException("release");
            DataTable dataTable = GetDataTable(release);
            string name = CreateExportFile(dataTable, "ReleaseDrops.csv");
            Process.Start(name);
        }

        public static void ExportToCSV(Project project)
        {
            if (project == null) throw new ArgumentNullException("project");
            DataTable dataTable = GetDataTable(project);
            string name = CreateExportFile(dataTable, "AllDrops.csv");
            Process.Start(name);
        }

        private static string CreateExportFile(DataTable dataTable, string name)
        {
            CSVFileWriter csvFileWriter = new CSVFileWriter(name);
            csvFileWriter.WriteFromDataTable(dataTable);
            csvFileWriter.Close();
            return name;
        }

       
        public static DataTable GetDataTable(Release release)
        {
            if (release == null) throw new ArgumentNullException("release");
            EditableDataSetProvider editableDataSetProvider = new EditableDataSetProvider(release.Drops);
            DataTable dataTable = editableDataSetProvider.GetDataTable(ClassDef.Get<Drop>().UIDefCol["DropsList"].UIGrid);
            //TODO: fix DateTime display- set to dd MMM yyyy in Firestarter, 
            //but does not appear in excel column in that form.
            //foreach (DataRow row in dataTable.Rows) 
            //TODO: review how to solve the DateTime display
            //{
            //    object value = row[3];
            //    DateTime dateTime = Convert.ToDateTime(value);
            //    row[3] = dateTime.ToString("dd MMM yyyy");
            //}
            dataTable.Columns.Remove("HABANERO_OBJECTID");
            return dataTable;
        }

        public static DataTable GetDataTable(Project project)
        {
            if (project == null) throw new ArgumentNullException("project");
            DataTable dataTable = new DataTable();
            if (project.Releases.Count == 0) return null;
            foreach (Release release in project.Releases)
            {
                dataTable.Merge(GetDataTable(release));
            }
            return dataTable;
        }

        public static DataTable GetCompletionStatusTable(Release release)
        {
            if (release == null) throw new ArgumentNullException("release");
            EditableDataSetProvider editableDataSetProvider;
            DataTable dataTable = new DataTable();
            for (int index = 0; index < release.Drops.Count; index++)
            {
                Drop drop = release.Drops[index];
                //Adds rows of use case names and use case points assigned 
                editableDataSetProvider = new EditableDataSetProvider(drop.UseCases);
                DataTable dataTableDrop =
                    editableDataSetProvider.GetDataTable(ClassDef.Get<UseCase>().UIDefCol["CompletionStatus"].UIGrid);
                dataTable.Merge(dataTableDrop);
                //Add drop columns
                dataTable.Columns.Add(drop.Name);
            }
            dataTable.Columns.Remove("HABANERO_OBJECTID");
            //add drop info into rows
            object[] endDates = new object[release.Drops.Count + 2];
            object[] assignedPoints = new object[release.Drops.Count + 2];
            int i = 2;
            foreach (Drop drop in release.Drops)
            {
                endDates[i] = drop.EndDate;
                assignedPoints[i] = drop.PointsAssigned;
                i++;
            }
            dataTable.Rows.Add(endDates);
            dataTable.Rows.Add(assignedPoints);

            return dataTable;
        }

        public static void ExportCompletionStatusToCSV(Release release)
        {
            if (release == null) throw new ArgumentNullException("release");
            DataTable dataTable = GetCompletionStatusTable(release);
            string name = CreateExportFile(dataTable, "CompletionStatus.csv");
            Process.Start(name);
        }

        public static void UglyGetCompletionStatusTable(Release release)
        {
            if (release == null) throw new ArgumentNullException("release");
            CSVFileWriter csvFileWriter = new CSVFileWriter("CompletionStatus.csv");
            DataTable dataTable = new DataTable();

            //add all column headings
            dataTable.Columns.Add(" ");//usecase name
            dataTable.Columns.Add("  ");//usecase points assigned
            foreach (Drop drop in release.Drops)//each drop gets a column
            {
                dataTable.Columns.Add(drop.Name);
            }
            //add drop info into rows
            object[] endDates = new object[release.Drops.Count + 2];
            object[] assignedPoints = new object[release.Drops.Count + 2];
            endDates[0] = "";
            endDates[1] = "Points";
            assignedPoints[0] = "Total Points";
            assignedPoints[1] = "";
            int i = 2;
            foreach (Drop drop in release.Drops)
            {
                endDates[i] = drop.EndDate;
                assignedPoints[i] = drop.PointsAssigned;
                i++;
            }
            dataTable.Rows.Add(endDates);
            dataTable.Rows.Add(assignedPoints);

            foreach (Drop drop in release.Drops.Clone())
            {
                foreach (UseCase useCase in drop.UseCases)
                {
                    object[] row = new object[2 + release.Drops.Count];
                    row[0] = useCase.UseCaseName;
                    row[1] = useCase.PointsAssigned;
                    int j = 2;
                    foreach (Drop drop1 in release.Drops.Clone())
                    {
                        if (useCase.DropID == drop1.DropID)
                        {
                            row[j] = "Y";
                        }
                        j++;
                    }
                    dataTable.Rows.Add(row);
                }
            }
            csvFileWriter.WriteFromDataTable(dataTable);
            csvFileWriter.Close();
            Process.Start("CompletionStatus.csv");
        }
    }
}